//
//  Utils.cpp
//  fingeradvanture
//
//  Created by shuaibincheng on 16/5/1.
//
//

#include "Utils.hpp"
#include "GB2ShapeCache-x.h"
#include "Box2D/Box2D.h"
#include "Monster.h"

int Utils::random_int(int max)
{
    srand((unsigned)time(NULL) + rand());
    auto value = (rand() / (RAND_MAX + 1.0));
    return (int)(max * value);
}

Node *Utils::createTestTroop(b2World* world, MonsterType type)
{
    Size visibleSize = Director::getInstance()->getVisibleSize();
    
    
//    auto ballOne=Sprite::create();
//    ballOne->setContentSize(Size(60, 60));
//    ballOne->setPosition(visibleSize.width/2,visibleSize.height/2);
//    //创建物体，并且物体的形状为圆形，第一参数为半径，第二个参数为物体材质
//    //第三个参数为边的厚度,就是在Debug模式下看到的物体外面线条的厚度，默认为0
//    PhysicsBody* ballBodyOne=PhysicsBody::createCircle(ballOne->getContentSize().width/2,PHYSICSBODY_MATERIAL_DEFAULT, Vec2(1,1));
//    //
//    //ballBodyOne->setCategoryBitmask(1);
//    
//    //是否设置物体为静态
//    //ballBodyOne->setDynamic(false);
//    //设置物体的恢复力
//    ballBodyOne->getShape(0)->setRestitution(1.0f);
//    //设置物体的摩擦力
//    ballBodyOne->getShape(0)->setFriction(0.0f);
//    //设置物体密度
//    ballBodyOne->getShape(0)->setDensity(1.0f);
//    //设置质量
//    //ballBodyOne->getShape(0)->setMass(5000);
//    //设置物体是否受重力系数影响
//    ballBodyOne->setGravityEnable(false);
//    ballBodyOne->setContactTestBitmask(0XFFFFFFFF);
//    
//    //设置物体的冲力
//    //Vect force=Vect(500000.0f, 500000.0f);
//    //ballBodyOne->applyImpulse(force);
//    //把物体添加到精灵中
//    ballOne->setPhysicsBody(ballBodyOne);
//    //设置标志
//    ballOne->setTag(1);
//    
//    ballOne->setPosition(visibleSize.width / 2, visibleSize.height
//                         + (ballOne->getContentSize().height / 2));
//    
//    auto mt = MoveTo::create(10, Vec2(visibleSize.width / 2, -10 - ballOne->getContentSize().height / 2));
//    
//    ballOne->runAction(mt);
    
//    string name = names[rand()%7];
//    
//    CCSprite *sprite = CCSprite::spriteWithFile((name+".png").c_str());
//    sprite->setPosition(p);
//    addChild(sprite);
//    
//    b2BodyDef bodyDef;
//    bodyDef.type = b2_dynamicBody;
//    
//    bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
//    bodyDef.userData = sprite;
//    b2Body *body = world->CreateBody(&bodyDef);
//    
//    // add the fixture definitions to the body
//    
//    GB2ShapeCache *sc = GB2ShapeCache::sharedGB2ShapeCache();
//    sc->addFixturesToBody(body, name.c_str());
//    sprite->setAnchorPoint(sc->anchorPointForShape(name.c_str()));
    
    //SpriteFrameCache* cache =SpriteFrameCache::sharedSpriteFrameCache();
    //cache->addSpriteFramesWithFile("elements.plist",TextureCache::sharedTextureCache()->addImage("elements.png"));
    
    
    //std::string str = String::createWithFormat("monster_%d", (rand()%5+1))->getCString();
    //cocos2d::Sprite* enemy = Sprite::createWithSpriteFrameName(str+".png");
    
    Sprite* enemy = Monster::create(world, type)->getMonster();
    
    enemy->setPosition(visibleSize.width / 2, visibleSize.height /2 /*+ (enemy->getContentSize().height / 2)*/);
//    auto movAction = MoveTo::create(10, Vec2(visibleSize.width / 2, -10 - enemy->getContentSize().height / 2));
    
//    enemy->setRotation(0.0f);
//    auto rotationAction = Repeat::create(RotateBy::create(5, 360), 10);
//
//    auto mt = Spawn::create(rotationAction/*,movAction*/,NULL);
//    
//    enemy->runAction(mt);
    
    
    //b2BodyDef bodyDef;
    //bodyDef.type = b2_dynamicBody;
    
    //bodyDef.position.Set(enemy->getPositionX()/PTM_RATIO, enemy->getPositionY()/PTM_RATIO);
    //bodyDef.userData = enemy;
    //b2Body *body = world->CreateBody(&bodyDef);
    
    // add the fixture definitions to the body
    
    //GB2ShapeCache *sc = GB2ShapeCache::sharedGB2ShapeCache();
    //sc->addFixturesToBody(body, str.c_str());
    //enemy->setAnchorPoint(sc->anchorPointForShape(str.c_str()));
    
    
    return enemy;
}

Node *Utils::createTestHero(b2World* world)
{
    //cocos
    Sprite* hero = Sprite::create();
    hero->setTextureRect(Rect(0,0,0.5*2*PTM_RATIO,0.5*2*PTM_RATIO)); //宽高要与body的身躯一致
    hero->setOpacity(0);
    //box2d,单位是米
    b2BodyDef def;
    def.position = b2Vec2(hero->getPositionX()/PTM_RATIO, hero->getPositionY()/PTM_RATIO);
    //def.linearVelocity = b2Vec2(0,10); //指定初始线性速度
    def.type = b2_dynamicBody;
    
    b2Body* body = world->CreateBody(&def);
    
    b2PolygonShape shape;
    shape.SetAsBox(0.5,0.5);//默认指半宽和半高为中心，上下左右都是半
    
    
    //创建该物体的身躯，使其可以与其它物体产生相互作用
    b2FixtureDef fixDef;
    fixDef.density = 1;//密度
    fixDef.friction = 0.3;//摩擦系数
    fixDef.shape = &shape;//指定图形区域
    body->CreateFixture(&fixDef);
    
    body->SetUserData(hero);//将图形与Body绑定起来，将Sprite的值传给body
    
    
    return hero;
}
